package servlet;

import controller.ControllerAmministrazioneLocal;
import controller.ControllerComuniLocal;
import java.io.IOException;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import utilities.Utilities;

/**
 * Gateway che smista tutte le richieste HTTP alle servlet di competenza in base
 * a un parametro action che è presente in tutte le richieste.
 * @author ale
 */
public class Controller extends HttpServlet {

    @EJB
    private ControllerAmministrazioneLocal controllerAmministrazione;
    @EJB
    private ControllerComuniLocal controllerComuni;

    /**
     * Operazioni che devono essere svolte all'avvio del sistema.
     * Il metodo crea il database dei comuni, se non presenti
     * e l'utente Amministratore, se non presente.
     * @throws ServletException 
     */
    @Override
    public void init() throws ServletException {
        super.init();
        controllerComuni.creaComuni();
        controllerAmministrazione.creaAmministratore();
    }

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {        
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        
        String action = request.getParameter("action");

        if (action == null) {
            Utilities.errore("Nessuna action specificata alla Controller", request, response);
        } else if (action.equals("mostraGestionePopup")
                || action.equals("mostraElencoUtenti")
                || action.equals("mostraElencoAnnunci")
                || action.equals("cancellaAnnuncio")
                || action.equals("bannaUtente")
                || action.equals("promuoviAModeratore")
                || action.equals("declassaDaModeratore")) {
            fwdAmministrazione(request, response);
        } else if (action.equals("mostraInserisciAnnuncio")
                || action.equals("mostraModificaSpesaCreatorePopup")
                || action.equals("inserisciAnnuncio")
                || action.equals("annuncioEsistente")
                || action.equals("accettaAdesione")
                || action.equals("rifiutaAdesione")
                || action.equals("accettaArticolo")
                || action.equals("rifiutaArticolo")
                || action.equals("rimuoviAnnuncio")
                || action.equals("chiudiAnnuncio")
                || action.equals("modificaSpesaCreatore")) {
            fwdCreazioneAnnuncio(request, response);
        } else if (action.equals("linkFb")
                || action.equals("postFb")
                || action.equals("registerFb")
                || action.equals("loginFb")
                || action.equals("tokenFb")) {
            fwdFb(request, response);
        } else if (action.equals("mostraFeedbackPopup")
                || action.equals("lasciaFeedbackCreatore")
                || action.equals("lasciaFeedbackIscritto")) {
            fwdFeedback(request, response);
        } else if (action.equals("mostraMappaPopup")
                || action.equals("indirizzoRiconosciuto")) {
            fwdGMaps(request, response);
        } else if (action.equals("mostraAggiungiArticoloPopup")
                || action.equals("iscrivitiAnnuncio")
                || action.equals("modificaAdesioneAnnuncio")
                || action.equals("salvaAdesioneInCorso")
                || action.equals("annullaAdesioneInCorso")
                || action.equals("rimuoviArticolodaAdesioneTemporanea")
                || action.equals("aggiungiArticoloadAdesioneTemporanea")
                || action.equals("disiscriviDaAnnuncio")
                || action.equals("checkIscrittoSito")) {
            fwdIscrizioneAnnuncio(request, response);
        } else if (action.equals("mostraLoginPopup")
                || action.equals("loginSito")
                || action.equals("loginOpenId")
                || action.equals("logout")) {
            fwdLogInOut(request, response);
        } else if (action.equals("mostraAnnunciCreati")
                || action.equals("mostraAnnunciIscritto")
                || action.equals("mostraAnnunciRegione")
                || action.equals("mostraAnnunciInScadenza")
                || action.equals("mostraAnnuncioEsistente")
                || action.equals("mostraDettagliAnnuncio")) {
            fwdMostraAnnuncio(request, response);
        } else if (action.equals("registraUtenteSocial")
                || action.equals("reallyLoginOpenId")
                || action.equals("authDoneRegistrazione")
                || action.equals("authDoneLogin")) {
            fwdOpenId(request, response);
        } else if (action.equals("mostraPagaAdesione")
                || action.equals("pagamentoEseguito")
                || action.equals("pagamentoAnnullato")) {
            fwdPagamento(request, response);
        } else if (action.equals("mostraProfilo")
                || action.equals("mostraModificaInfoProfiloPopup")
                || action.equals("modificaInfoProfilo")) {
            fwdProfilo(request, response);
        } else if (action.equals("mostraRicercaAvanzata")
                || action.equals("mostraRicercaPopup")
                || action.equals("cercaAnnuncio")
                || action.equals("ricercaAvanzataAnnuncio")) {
            fwdRicerca(request, response);
        } else if (action.equals("mostraRegistrazioneUtenteSito")
                || action.equals("mostraRegistrazionePopup")
                || action.equals("mostraDimenticataPasswordPopup")
                || action.equals("utenteEsistente")
                || action.equals("changePassword")
                || action.equals("validateUser")
                || action.equals("mailPassword")
                || action.equals("forgetPassword")
                || action.equals("registerUser")) {
            fwdUserRegistrationServlet(request, response);
        }


    } //fine processRequest

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

    private void fwdAmministrazione(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Utilities.forward("/AmministrazioneServlet", request, response);
    }

    private void fwdCreazioneAnnuncio(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Utilities.forward("/CreazioneAnnuncioServlet", request, response);
    }

    private void fwdFb(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Utilities.forward("/FbServlet", request, response);
    }

    private void fwdFeedback(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Utilities.forward("/FeedbackServlet", request, response);
    }

    private void fwdGMaps(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Utilities.forward("/GMapsServlet", request, response);
    }

    private void fwdIscrizioneAnnuncio(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Utilities.forward("/IscrizioneAnnuncioServlet", request, response);
    }

    private void fwdLogInOut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Utilities.forward("/LogInOutServlet", request, response);
    }

    private void fwdMostraAnnuncio(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Utilities.forward("/MostraAnnuncioServlet", request, response);
    }

    private void fwdOpenId(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Utilities.forward("/OpenIdServlet", request, response);
    }

    private void fwdPagamento(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Utilities.forward("/PagamentoServlet", request, response);
    }

    private void fwdProfilo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Utilities.forward("/ProfiloServlet", request, response);
    }

    private void fwdRicerca(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Utilities.forward("/RicercaServlet", request, response);
    }

    private void fwdUserRegistrationServlet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Utilities.forward("/UserRegistrationServlet", request, response);
    }
    
}